﻿@model DoeLibsMVC.Models.EditLoanableModel
@{
    ViewBag.Title = "Add new loanable";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2 class="centeredHeaderText">Add a new title / loanable</h2>
<div class="formDesignDiv">

    @using (Html.BeginForm("Add", "Loanable", FormMethod.Post, new { id = "addLoanableForm", enctype = "application/x-www-form-urlencoded" }))
    {
        <table class="formTable">
		    <tbody>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.Title)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.Title, new { @id = "titleTitle", @class = "generalTxtBox", @placeholder = "enter title..." })
                            @Html.ValidationMessageFor(model => model.Title)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.ISBN13)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.ISBN13, new { @id = "titleISBN13", @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.ISBN13)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.ISBN10)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.ISBN10, new { @id = "titleISBN10", @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.ISBN10)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.Authors)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.Authors, new { @id = "titleAuthors", @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.Authors)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.Editors)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.Editors, new { @id = "titleEditors", @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.Editors)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.FirstEditionYear)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.FirstEditionYear, new { @id = "titleFirstEditionYear", @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.FirstEditionYear)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.Edition)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.Edition, new { @id = "titleEdition", @class = "generalTxtBox", Value = Model.Edition == 0 ? "" : Model.Edition.ToString() })
                            @Html.ValidationMessageFor(model => model.Edition)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.PublicationYear)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.PublicationYear, new { @id = "titlePublicationYear", @class = "generalTxtBox", Value = Model.PublicationYear == 0 ? "" : Model.PublicationYear.ToString() })
                            @Html.ValidationMessageFor(model => model.PublicationYear)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.Publisher)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.Publisher, new { @id = "titlePublisher", @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.Publisher)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.Topics)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.Topics, new { @id = "titleTopics", @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.Topics)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.Label("Add title only")
                    </td>
                    <td>
                        <div>
                            @Html.CheckBox("onlyTitleInformation", new { id = "chbxOnlyTitleInformation", onClick = "onOnlyTitleInformationClicked()", style = "float: left;" })
                        </div>
                    </td>
                </tr>
                <tr class="loanableInformation">
                    <td>
                        @Html.LabelFor(model => model.Barcode)
                    </td>
                    <td>
                        <div id="divBarcode">
                            @Html.TextBoxFor(model => model.Barcode, new { @id = "titleBarcode", @class = "generalTxtBox", onBlur = "checkIdentifier()" })
                            <input type="button" onclick="insertFreeBarcode()" value="generate identifier" class="generalBtn" />
                            @Html.ValidationMessageFor(model => model.Barcode, null, new {id = "validationMessageBarcode" })
                        </div>
                    </td>
                </tr>
                <tr class="loanableInformation">
                    <td>
                        @Html.LabelFor(model => model.Category)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.Category, new { @id = "titleLocationCategory", @class = "generalTxtBox" })
                            @Html.ValidationMessageFor(model => model.Category)
                        </div>
                    </td>
                </tr>
                <tr class="loanableInformation">
                    <td>
                        @Html.LabelFor(model => model.Location)
                    </td>
                    <td>
                        <div>
                            @Html.TextBoxFor(model => model.Location, new { @id = "titleLocation", @class = "generalTxtBox", Value = User.Room })
                            @Html.ValidationMessageFor(model => model.Location)
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>

                    </td>
                    <td>
                        <input type="submit" value="save" class="generalBtn"/>
                    </td>
                </tr>
            </tbody>
        </table>        
       
    }
</div>

<script src="~/Content/js/Autocomplete.js" type="text/javascript"></script>
<script type="text/javascript">
    function checkIdentifier() {
        $.ajax({
            dataType: "json",
            method: "GET",
            url: '/api/loanable?doeLibId=' + $("#titleBarcode").val()
        })
            .done(function (data) {
                $("#validationMessageBarcode").text("Identifier already in use").removeClass('field-validation-valid').addClass('field-validation-error');
            }).fail(function (data) {
                $("#validationMessageBarcode").text("");
            });
    }

    function insertFreeBarcode() {
        $.ajax({
            dataType: "json",
            method: "GET",
            url: '/api/loanable?generateFreeDoeLibsId',
            success: function (data) {
                $("#titleBarcode").val(data);
                $("#validationMessageBarcode").text("");
            }
        });
    }

    function isOnlyTitleInformationCheckBoxChecked() {
        return $('#chbxOnlyTitleInformation').is(':checked');
    }

    function onOnlyTitleInformationClicked() {
        if (isOnlyTitleInformationCheckBoxChecked()) {
            $(".loanableInformation").hide();
        } else {
            $(".loanableInformation").show();
        }
    }

    $("#addLoanableForm").submit(function () {
        console.log(getFormData($('#addLoanableForm')));
        
        if (isOnlyTitleInformationCheckBoxChecked()) {
            url = "/api/title/";
            message = "Title added successfully";
        } else {
            url = "/api/loanable/";
            message = "Loanable added successfully";
        }

        console.log("POST form to: ", url);

        $.ajax({
            url: url,
            type: "POST",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", getAuthorizationHeader('@User.UserId', ''));
            },
            dataType: "json",
            data: getFormData($('#addLoanableForm'))
        }).done(function(data) {
            console.log("success");

            setHttpMessage(message, true);
            document.location = '@Url.Action("Get", "Title")/' + data.Title.TitleId;
        }).fail(function (data) {
            console.log("fail");

            clearAllErrorFields('addLoanableForm');
            console.log(data);

            if (data.status == 400) {
                console.log("bad request");

                addErrorsToFields("model.", data.responseJSON);
            } else {
                //servererror, etc.
                setHttpMessageDynamic("Technical problems. Please try again", false);
            }
        });

        return false;
    });

    //function checkIdentifier() {
    //    $.ajax({
    //        dataType: "json",
    //        method: "GET",
    //        url: '/api/loanable?doeLibId=' + $("#titleBarcode").val(),
    //        success: function (data) {
    //            if (data != null) {
    //                var label = $("<label>").text("Identifier already exist.");
    //                label.attr("id","error_BarcodeExist");

    //                $('#divBarcode').append(label);
    //            }
    //        }
    //    });
    //}

    //$("#titleBarcode").keyup(function () {

    //    if ($("#error_BarcodeExist") != null)
    //    {
    //        $("#error_BarcodeExist").remove();
    //    }

    //    var doeIdentifier = $.trim($('#titleBarcode').val());
    //    if (doeIdentifier !== "") {
    //        checkIdentifier();
    //    }
    //});


</script>